package Leetcode.数组字符串;

/**
 * @Author kirito
 * @Date 2023/10/16 10:45
 * @PackageName:Leetcode.数组字符串
 * @ClassName: 跳跃游戏2
 * @Description:
 * @Version 1.0
 */
public class 跳跃游戏2 {
    public int jump(int[] nums) {
        int ans = 0;
        int start = 0;
        int end = 1;
        while (end < nums.length) {
            int maxPos = 0;
            for (int i = start; i < end; i++) {
                //能跳到最远的距离
                maxPos = Math.max(maxPos, i + nums[i]);
            }
            start = end;      //下一次起跳点范围开始的格子
            end = maxPos + 1; //下一次起跳点范围结束的格子
            ans++;
        }
        return ans;
    }

    public int jump2(int[] nums) {
        int ans = 0;
        int maxPos = 0;
        int end = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            maxPos = Math.max(nums[i], maxPos);
            if (i == end) {
                end = maxPos;
                ans++;
            }
        }
        return ans;
    }
}
